package cn.xingyun.pickaxe_loli_new.mixin;

import cn.xingyun.pickaxe_loli_new.init.NewLoliPickaxeModItems;
import cn.xingyun.pickaxe_loli_new.item.LolipickaxeItem;
import net.minecraft.core.NonNullList;
import net.minecraft.server.commands.ClearInventoryCommands;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.PickaxeItem;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import javax.annotation.Nonnull;
import java.util.List;
import java.util.function.Predicate;

@Mixin(Inventory.class)
public abstract class InventoryMixin {
    @Shadow public abstract boolean contains(ItemStack p_36064_);

    @Shadow public abstract ItemStack getItem(int p_35991_);

    @Shadow @Final public NonNullList<ItemStack> items;

    @Inject(method = "removeItem(Lnet/minecraft/world/item/ItemStack;)V",at = @At("HEAD"),cancellable = true)
    public void removeItem(ItemStack p_36058_, CallbackInfo ci){
        if(p_36058_.getItem() instanceof PickaxeItem){
            ci.cancel();
        }
    }
    @Inject(method = "replaceWith",at = @At("HEAD"),cancellable = true)
    public void replaceWith(Inventory p_36007_, CallbackInfo ci){
        if (contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())){
            ci.cancel();
        }
    }
    @Inject(method = "setItem",at = @At("HEAD"),cancellable = true)
    public void setItem(int p_35999_, ItemStack p_36000_, CallbackInfo ci){
        if (getItem(p_35999_).is(NewLoliPickaxeModItems.LOLIPICKAXE.get())){
            ci.cancel();
        }
    }
    @Inject(method = "clearContent",at = @At("HEAD"),cancellable = true)
    public void clearContent(CallbackInfo ci){
        if (contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())){
            ci.cancel();
        }
    }
    @Inject(method = "dropAll",at = @At("HEAD"),cancellable = true)
    public void dropAll(CallbackInfo ci){
        if (contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())){
            ci.cancel();
        }
    }
    @Inject(method = "clearOrCountMatchingItems",at = @At("HEAD"),cancellable = true)
    public void clear(Predicate<ItemStack> p_36023_, int p_36024_, Container p_36025_, CallbackInfoReturnable<Integer> cir){
        if (contains(NewLoliPickaxeModItems.LOLIPICKAXE.get().getDefaultInstance())){
            cir.setReturnValue(0);
        }
    }
    @Mixin(NonNullList.class)
    public abstract static class NonNullListMixin<E> {
        @Shadow @Final private List<E> list;

        @Shadow @Nonnull public abstract E get(int p_122791_);

        @Inject(method = "clear",at = @At("HEAD"),cancellable = true)
        public void clear(CallbackInfo ci){
            if (list.contains(NewLoliPickaxeModItems.LOLIPICKAXE.get())){
                ci.cancel();
            }
        }
    }
}
